import os
import shutil


def moveDirFils(source_dir, targe_dir):
    shutil.move(source_dir, targe_dir)


def moveFilesToUserRoot(batch_task, TheerDobj_path, nii_pred, org_pred, ans_path, tmp_root):
    user_root = batch_task.userRoot

    user_ana_path = os.path.join(user_root, 'analysis')

    user_viewlabel_path = os.path.join(user_root, 'labels', str(batch_task.labelSetId), 'view_img')
    user_orglabel_path = os.path.join(user_root, 'labels', str(batch_task.labelSetId), 'org_img')
    user_obj_path = os.path.join(user_root, 'labels', str(batch_task.labelSetId), 'obj')

    # make dir
    if not os.path.exists(user_ana_path):
        os.makedirs(user_ana_path)
    if not os.path.exists(user_viewlabel_path):
        os.makedirs(user_viewlabel_path)
    if not os.path.exists(user_orglabel_path):
        os.makedirs(user_orglabel_path)
    if not os.path.exists(user_obj_path):
        os.makedirs(user_obj_path)

    # move files
    moveDirFils(TheerDobj_path, user_obj_path)
    moveDirFils(nii_pred, user_viewlabel_path)
    moveDirFils(org_pred, user_orglabel_path)

    # move ana
    labelset_id_ana_path = os.path.join(user_ana_path, str(batch_task.labelSetId))
    if not os.path.exists(labelset_id_ana_path):
        os.makedirs(labelset_id_ana_path)

    moveDirFils(ans_path, labelset_id_ana_path)
    gobel_ana_path = os.path.join(labelset_id_ana_path, 'GlobalStatistics.json')
    moveDirFils(gobel_ana_path, user_ana_path)

    # remove tmp
    shutil.rmtree(tmp_root)

    return user_viewlabel_path, user_orglabel_path, user_obj_path, user_ana_path

